
AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include( 'shared.lua' )
util.PrecacheSound( "Harvest/shockwave.wav" )

function ENT:Initialize()

	self.Entity:SetName("Shockwave Warhead")
	self.Entity:SetModel( "models/items/combine_rifle_ammo01.mdl" )

	local phys = self.Entity:GetPhysicsObject()
	if (phys:IsValid()) then
		phys:Wake()
		phys:EnableGravity(false)
		phys:EnableDrag(false)
		phys:EnableCollisions(false)
	end

	self.PhysObj = self.Entity:GetPhysicsObject()
	self.STime = CurTime()
	self.BTime = self.STime + 2

	self.Gas = ents.Create("env_smoketrail")
	self.Gas:SetAngles( self.Entity:GetAngles()  )
	self.Gas:SetPos( self.Entity:GetPos() )
	self.Gas:SetParent( self.Entity )
	self.Gas:SetKeyValue("startcolor", "180 140 80")
	self.Gas:SetKeyValue("endcolor", "200 170 129")
	self.Gas:SetKeyValue("startsize","50")
	self.Gas:SetKeyValue("endsize","100")
	self.Gas:SetKeyValue("lifetime", "1")
	self.Gas:SetKeyValue("opacity", ".5")
	self.Gas:SetKeyValue("spawnrate","5")
	self.Gas:Spawn()
	self.Gas:Activate()
	
end

function ENT:PhysicsUpdate()

end


function ENT:SpawnFunction( ply, tr )

	if ( !tr.Hit ) then return end

	local SpawnPos = tr.HitPos + tr.HitNormal * 36

	local ent = ents.Create( "swave_head" )
	ent:SetPos( SpawnPos )
	ent:Spawn()
	ent:Activate()
	return ent
end

function ENT:Think()
	self.Entity:SetColor(0,0,0,0)
	

	
	if (self.PreDust != true) then

		local expl=ents.Create("env_explosion")
		expl:SetPos(self.Entity:GetPos())
		expl:SetName("Missile")
		expl:SetParent(self.Entity)
		expl:SetOwner(self.Entity:GetOwner())
		expl:SetKeyValue("iMagnitude","100");
		expl:SetKeyValue("iRadiusOverride", 50)
		expl:Spawn()
		expl:Activate()
		expl:Fire("explode", "", 0)
		expl:Fire("kill","",0)
		self.Exploded = true

		local p = 0

		for p = 0, 15, 1 do
			local DSpray = ents.Create( "swavedust" )
			if ( !DSpray:IsValid() ) then return end
			DSpray:SetPos( self.Entity:GetPos() )
			DSpray:Spawn()
			DSpray:SetOwner( self.Entity )
			DSpray:Fire( "kill", "", 4)
			DSpray.PhysObj:SetVelocity( Vector( math.random( -50, 50 ), math.random( -50, 50 ), math.random( 10, 100 ) ) )
		end
		self.PreDust = true
	end

	if (CurTime() >= self.BTime && self.Blasted != true) then
		self.Blasted = true
		self.Entity:EmitSound( "Harvest/shockwave.wav", 1000, 100 )
		local x = 0

		for x = 0, 359, 5 do
			local Wave = ents.Create( "swave" )
			if ( !Wave:IsValid() ) then return end
			Wave:SetPos( self.Entity:GetPos() )
			Wave:SetAngles( Angle(0, x, 0) )
			Wave:Spawn()
			Wave:SetOwner( self.Entity )
			Wave:Fire( "kill", "", 3)
			Wave.PhysObj:SetVelocity( Wave.Entity:GetForward() * 1000 )
		end
		self.Entity:Remove()
	end


end

function ENT:PhysicsCollide( data, physobj )
	
end

function ENT:OnTakeDamage( dmginfo )
	
end

function ENT:Use( activator, caller )

end